package cn.itcast.user.service.impl;

import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.pojo.user.User;
import cn.itcast.hmall.utils.ThreadLocalUtil;
import cn.itcast.user.mapper.FindUserMapper;
import cn.itcast.user.mapper.UserMapper;
import cn.itcast.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, Address> implements UserService {


    @Autowired
    private FindUserMapper userMapper;
    /**
     * 根据用户id 查询地址列表
     * @return
     */
    @Override
    public List<Address> queryAddressByUid() {
        //获取当前登录用户id
        Long userId = ThreadLocalUtil.getUserId();
        //创建查询条件
        LambdaQueryWrapper<Address> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Address::getUserId,userId);

        Page<Address> page = new Page<>();
        Page<Address> addressPage = this.page(page, wrapper);
        List<Address> addressList = addressPage.getRecords();
        return addressList;
    }

    @Override
    public Address queryAddressById(Long addressId) {

        Address address = this.getById(addressId);
        return address;
    }

    /**
     * 根据id查询用户
     * @param uid
     * @return
     */
    @Override
    public User findUserById(Long uid) {
        User user = userMapper.selectById(uid);
        return user;
    }

    /**
     * 根据id修改用户
     * @param user
     * @return
     */
    @Override
    public ResultDTO updateUserById(User user) {
        int i = userMapper.updateById(user);
        if (i>0){
            return ResultDTO.ok();
        }
        return ResultDTO.error();
    }
}
